package com.pospi.xstore.ui.cash_ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.jzxiang.pickerview.TimePickerDialog;
import com.jzxiang.pickerview.data.Type;
import com.jzxiang.pickerview.listener.OnDateSetListener;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.pospi.xstore.R;
import com.pospi.xstore.dao.GoodsDao;
import com.pospi.xstore.dto.GoodsDto;
import com.pospi.xstore.dto.ReceiveBeen;
import com.pospi.xstore.http.LoadServer;
import com.pospi.xstore.ui.base.BaseActivity;
import com.pospi.xstore.ui.count.CountsActivity;
import com.pospi.xstore.util.Api;
import com.pospi.xstore.util.GetData;
import com.pospi.xstore.util.SaveOrderInfo;

import org.apache.http.Header;
import org.json.JSONArray;
import org.json.JSONException;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by acer on 2018/6/20.
 */

public class ReceiveGoodsActivity extends BaseActivity implements OnDateSetListener {
    @Bind(R.id.lv_receive)
    ListView list_Receive;
//    @Bind(R.id.receive_date)
//    TextView receive_date;

    private List<ReceiveBeen> mList = new ArrayList<>();
    private ReceiveOrderAdpter adpter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentLayout(R.layout.activity_receive);
        setTitle("收货单");
        setMenuText("刷新");
        ButterKnife.bind(this);
//        receive_date.setText(GetData.getYYMMDDTime());
        init();
    }

    @Override
    public void onMenuClick(View view) {
        super.onMenuClick(view);
        getReceiveOrder();
        new AlertDialog.Builder(this).setMessage("刷新成功")
                .setPositiveButton("是", null)
                .show();
    }

    private void init() {
        adpter = new ReceiveOrderAdpter();
        list_Receive.setAdapter(adpter);
        getReceiveOrder();
        list_Receive.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent intent = new Intent(ReceiveGoodsActivity.this, ReceiveActivity.class);
                intent.putExtra("billno", mList.get(i).getSid());
                intent.putExtra("type", mList.get(i).getType());
                startActivity(intent);
            }
        });
//        receive_date.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                TimePickerDialog dialogYearMonthDay = new TimePickerDialog.Builder()
//                        .setType(Type.YEAR_MONTH_DAY)
//                        .setTitleStringId("选择日期")
//                        .setWheelItemTextSize(16)
//                        .setCallBack(ReceiveGoodsActivity.this)
//                        .build();
//                dialogYearMonthDay.show(getSupportFragmentManager(), "YEAR_MONTH_DAY");
//            }
//        });

    }

    @Override
    public void onDateSet(TimePickerDialog timePickerView, long millseconds) {
        Date date = new Date(millseconds);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String time = format.format(date);
//        receive_date.setText(time);
    }

    private class ReceiveOrderAdpter extends BaseAdapter{

        @Override
        public int getCount() {
            return mList.size();
        }

        @Override
        public Object getItem(int i) {
            return mList.get(i);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            Holder holder;
            if (view == null) {
                holder = new Holder();
                view = LayoutInflater.from(getApplicationContext()).inflate(R.layout.receive_order_item, null);
                holder.tvSid = (TextView) view.findViewById(R.id.tv_receive_id);
                holder.tvName = (TextView) view.findViewById(R.id.tv_receive_name);
                holder.tvDate = (TextView) view.findViewById(R.id.tv_receive_date);
                holder.tvType = (TextView) view.findViewById(R.id.tv_receive_type);
                view.setTag(holder);

            } else {
                holder = (Holder) view.getTag();
            }
            ReceiveBeen been = mList.get(i);
            holder.tvSid.setText(been.getMId());
            holder.tvName.setText(been.getSuName());
            holder.tvDate.setText(been.getMTime());
            holder.tvType.setText(been.getType().equals("1") ? "采购入库" : "调拨入库");

            return view;
        }
    }
    class Holder{
        TextView tvSid;
        TextView tvName;
        TextView tvDate;
        TextView tvType;
    }
    private void getReceiveOrder(){
        RequestParams params = new RequestParams();
        params.put("sh_id", getSharedPreferences("StoreMessage", MODE_PRIVATE).getString("Id", ""));
        LoadServer.getConnect(getApplicationContext(), Api.SYNC_RECEIVE_LIST, params, new AsyncHttpResponseHandler() {
                    @Override
                    public void onSuccess(int code, Header[] headers, byte[] bytes) {
                        if (code == 200) {
                            Log.i("responseBody", new String(bytes));
                            try {
                                List<ReceiveBeen> beenList = new ArrayList<>();
                                JSONArray array = new JSONArray(new String(bytes));
                                for (int i = 0;i<array.length();i++) {
                                    ReceiveBeen been = new ReceiveBeen();
                                    been.setMId(array.getJSONObject(i).getString("billno"));
                                    been.setSuName(array.getJSONObject(i).getString("sup"));
                                    been.setType(array.getJSONObject(i).getString("type"));
                                    been.setSid(array.getJSONObject(i).getString("id"));
                                    been.setMTime(array.getJSONObject(i).getString("adddt"));
                                    beenList.add(been);
                                }
                                addData(beenList);
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }

                        }
                    }
                    @Override
                    public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {
                        Toast.makeText(ReceiveGoodsActivity.this, "网络请求失败", Toast.LENGTH_SHORT).show();
                    }
                }
        );
    }
    private void addData(List<ReceiveBeen> receiveBeens){
        mList.clear();
        mList.addAll(receiveBeens);
        adpter.notifyDataSetChanged();
    }
}
